#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "error.h"

#define	MAX_ERROR		256

void
print_error(short code)
{
	char msg[MAX_ERROR+1];
	
	switch (-code)
	{
			/* possible login errors */
		case ERROR_WRONG_LOGIN:
			strcpy(msg, "Wrong user or password.\n");
			break;
		case ERROR_NEED_LOGIN:
			strcpy(msg, "You are not logged in.\n");
			break;
			
			/* file errors */
		case ERROR_FILE_NOT_EXISTS:
			strcpy(msg, "File doesn\'t exist.\n");
			break;
			
			/* ticket errors */
		case ERROR_NO_TICKETS_LEFT:
			strcpy(msg, "You have no tickets left for today.\n");
			break;
		case ERROR_TICKET_NOT_EXISTS:
			strcpy(msg, "Ticket doesn\'t exist.\n");
			break;
		case ERROR_TICKET_LOST:
			strcpy(msg, "You tried to use your ticket many times before it "
				   "was enabled.\nYou lost your ticket!\n");
			break;
		case ERROR_TICKET_NA:
			strcpy(msg, "Ticket is not enabled yet.\n");
			break;
		case ERROR_TICKET_EXPIRED:
			strcpy(msg, "Sorry, the ticket already expired.\n");
			break;
		case ERROR_ALLRDY_DOWNLADING:
			strcpy(msg, "You are already downloading a file. Please wait "
				   "until the download is completed before requesting a new "
				   "ticket.\n");
			break;
			
			/* upload errors */
		case ERROR_UPLOAD_NO_SPACE:
			strcpy(msg, "No space left in the server. Sorry, upload not "
				   "authorized.\n");
			break;
		case ERROR_UPLOAD_FILE_EXISTS:
			strcpy(msg, "A file with the same filename already exists. "
				   "Try again with a diferent filename.\n");
			break;
		case ERROR_UPLOAD_INVALID_FILENAME:
			strcpy(msg, "The filename contains invalid characters\n");
			break;
			
			/* internal server error */
		case ERROR_SERVER_CRITIC:
			strcpy(msg, "Critical internal server error.\n");
			break;
		case ERROR_SERVER_FATAL:
			strcpy(msg, "Fatal internal server error.\n");
			break;
		case ERROR_CONNECTION_FAILURE:
			strcpy(msg, "Connection failure. Please try again.\n");
			break;
		case ERROR_OPCODE_CONFLICT:
			strcpy(msg, "Operation code conflict.\n");
			break;
		case ERROR_MAX_CONN_REACHED:
			strcpy(msg, "Maximum connections reached. Please wait a moment and "
				   "try again. You will not be penalized.\n");
			break;
			
			/* list errors */
		case ERROR_LIST_NO_CAT:
			strcpy(msg, "No such category.\n");
			break;
		case ERROR_LIST_NO_FILES:
			strcpy(msg, "No files in that category.\n");
			break;
		case ERROR_LIST_NO_USERS:
			strcpy(msg, "No registered users.\n");
			break;
		
		/* other errors */
		case ERROR_TIME_OUT:
			strcpy(msg, "Connection timed out.\n");
			break;
			
			/* any other error */
		default:
			strcpy(msg, "Unknown error.\n");
	}
	
	fprintf(stderr, msg);
	return;
}
